const Koa = require("koa");
const bodyParser = require("koa-bodyparser");
const cors = require("koa2-cors");
const userRouter = require("../router/user-router");
const browseRouter = require("../router/browse-router")
const answerRouter = require("../router/answer-router")
const app = new Koa();

// 使用koa2-cors插件解决跨域问题
app.use(
  cors({
    origin: function (ctx) {
      return "*"; // 允许来自所有域名请求
    },
    exposeHeaders: ["WWW-Authenticate", "Server-Authorization"],
    maxAge: 5,
    credentials: true,
    allowMethods: ["GET", "POST", "DELETE", "OPTIONS", "PUT"],
    allowHeaders: ["Content-Type", "Authorization", "Accept"],
  })
);

//注册koa-bodyparser中间件，用途：解析用户传来的JSON数据


app.use(bodyParser());
//用户登录模块请求接口
app.use(userRouter.routes());
app.use(userRouter.allowedMethods());

//浏览模块请求接口
app.use(browseRouter.routes())
app.use(browseRouter.allowedMethods())

//答题模块请求接口
app.use(answerRouter.routes())
app.use(answerRouter.allowedMethods())

module.exports = app;
